package o2o.web.actions.shop;

import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.shop.ShopMemory;
import o2o.cache.shop.ShopPositionMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.config.dao.UserDaoSql;
import o2o.dao.PO.PojoTables;
import o2o.company.model.ShopInfo;
import o2o.dao.PO.shop.ShopPositionInfo;
import o2o.dao.PO.user.UserInfo;
import o2o.system.data.PROMPTS;
import o2o.utils.MultilingualResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;

@Controller
public class ShopPositionController {

    //-------------------------------------------------------店主位置信息
    //店主位置更新
    @RequestMapping(method = RequestMethod.POST, value = "/shop/position/report")
    @ResponseBody
    public String shop_position(String token, Double lat, Double lng, String city, String address) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        ShopInfo shopInfo = ShopMemory.getInstance().getShopByUsername(user.getUsername());
        if (null == shopInfo) {
            return AjaxResult.errorResult("店铺不存在");
        }
        //更新配送员位置信息
        updateShopPosition(shopInfo.getId(), shopInfo.getOwnUsername(), lat, lng, city, address);

        return AjaxResult.successResult();
    }

    //获取店主的最新位置
    @RequestMapping(method = RequestMethod.POST, value = "/shop/position/get")
    @ResponseBody
    public String get_shop_position(String token, String shopUsername, Long shopId, String lang) {
        if (ValidateUtil.isNotID(shopId) && ValidateUtil.isNull(shopUsername)) {
//            return AjaxResult.errorResult("请选择要查看的店铺");
            return MultilingualResult.errorResultV2(PROMPTS.SELECT_SHOP_VIEW, lang);
        }
        if (ValidateUtil.isNotID(shopId)) {
            ShopInfo shopInfo = ShopMemory.getInstance().getShopByUsername(shopUsername);
            if (null == shopInfo) {
//                return AjaxResult.errorResult("店铺不存在");
                return MultilingualResult.errorResultV2(PROMPTS.SHOP_NOT_EXIST , lang);
            }
            shopId = shopInfo.getId();
        }

        ShopPositionInfo info = ShopPositionMemory.getInstance().get(shopId);

        return AjaxResult.successResult(info);
    }


    //-------------------------------

    //更新配送员位置
    public static boolean updateShopPosition(Long shopId, String username, Double lat, Double lng, String city, String address) {
        if (ValidateUtil.isNotID(shopId)) {
            return false;
        }
        if (ValidateUtil.isNull(username)) {
            return false;
        }
//        if (ValidateUtil.isNotAbsDouble(lat) || ValidateUtil.isNotAbsDouble(lng)) {
        if (ValidateUtil.isNotLatLng(lat, lng)) {
            return false;
        }

        ShopPositionInfo shopPositionInfo = ShopPositionMemory.getInstance().get(shopId);
        if (null == shopPositionInfo) { //没有该配送员信息 存入数据库
            StringBuilder sb = new StringBuilder();
            sb.append("insert into ").append(PojoTables.SHOP_POSITION_INFO).append(" (shopId,username,city,lat,lng,address,createTime,isWork) values(").append(shopId)
                    .append(",'").append(username).append("','").append(city).append("',").append(lat).append(",").append(lng)
                    .append(",'").append(address).append("',").append(DateUtils.getCurrTime()).append(",1)");
            try {
                UserDaoSql.getInstance().executeBySql(sb.toString());
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        //更新店铺的最新位置信息
        ShopPositionMemory.getInstance().updateLocal(shopId, username, "", city, "", address, lat, lng);

        return true;
    }
}
